package com.king.headline.fragment;

/**
 * Created by king on 2015/12/1.
 */

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.king.headline.R;
import com.king.headline.UI.RegistActivity;
import com.king.headline.UI.SettingActivity;
import com.king.headline.base.BaseFragment;
import com.king.headline.beans.UserInfo;
import com.king.headline.cache.DataCleanManager;
import com.king.headline.util.AccountManager;
import com.king.headline.util.Configuration;
import com.king.headline.util.CustomToast;
import com.king.headline.util.DialogUtils;
import com.king.headline.util.UIUtils;
import com.king.headline.util.Utils;
import com.king.headline.view.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ThreeFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.rl_head)
    RelativeLayout rl_head;
    @BindView(R.id.tv_nickName)
    TextView tv_nickName;
    @BindView(R.id.iv_chatHeadurl)
    CircleImageView iv_chatHeadurl;
    @BindView(R.id.ll_login)
    LinearLayout ll_login;
    @BindView(R.id.ll_nologin)
    LinearLayout ll_nologin;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.tv_regist)
    TextView tv_regist;
    @BindView(R.id.rl_clearn)
    RelativeLayout rl_clearn;
    @BindView(R.id.rl_version)
    RelativeLayout rl_version;
    @BindView(R.id.rl_contact)
    RelativeLayout rl_contact;
    @BindView(R.id.rl_aboutmy)
    RelativeLayout rl_aboutmy;
    @BindView(R.id.tv_cachesize)
    TextView tv_cachesize;
    @BindView(R.id.tv_VersionName)
    TextView tv_VersionName;

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.activity_three, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initData() {
        tv_VersionName.setText(Utils.getVersionName());
        rl_head.setOnClickListener(this);
        rl_aboutmy.setOnClickListener(this);
        rl_contact.setOnClickListener(this);
        rl_version.setOnClickListener(this);
        rl_clearn.setOnClickListener(this);
        try {
            String cachesize = DataCleanManager.getTotalCacheSize(mActivity);
            tv_cachesize.setText("当前缓存" + cachesize);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        UserInfo userInfo = AccountManager.getInstance().getUser();
        if (userInfo != null) {
            rl_head.setClickable(true);
            ll_nologin.setVisibility(View.GONE);
            ll_login.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(userInfo.nickname.trim())) {
                tv_nickName.setText(userInfo.getPhoneNumber());
            } else {
                tv_nickName.setText(userInfo.nickname);
            }

            UIUtils.getImageLoader().displayImage(Configuration.API_HOST + userInfo.chatHeadurl, iv_chatHeadurl);
        } else {
            rl_head.setClickable(false);
            tv_regist.setOnClickListener(this);
            tv_login.setOnClickListener(this);
            ll_nologin.setVisibility(View.VISIBLE);
            ll_login.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_head:
                startActivity(new Intent(mActivity, SettingActivity.class));
                break;
            case R.id.tv_login:
                AccountManager.goLogingActivity();
                break;
            case R.id.tv_regist:
                startActivity(new Intent(mActivity, RegistActivity.class));
                break;
            case R.id.rl_aboutmy:
                CustomToast.showToast("关于我们");
                break;
            case R.id.rl_contact:
                CustomToast.showToast("联系我们");
                break;
            case R.id.rl_version:
                CustomToast.showToast("已是最新版本");
                break;
            case R.id.rl_clearn:
                DialogUtils.showHintDialogTwo(mActivity, "确定清除本地缓存?", new DialogUtils.OnDialogListener() {
                    @Override
                    public void onOk(View v) {
                        // 退出的时候，清空缓存
                        DataCleanManager.clearAllCache(mActivity);
                        try {
                            String cachesize = DataCleanManager.getTotalCacheSize(mActivity);
                            tv_cachesize.setText("当前缓存" + cachesize);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCancel(View v) {

                    }
                });
                break;
        }


    }
}

